package com.lovo.sgproj.frame.studentmanage;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoFileChooser;
import com.lovo.netCRM.component.LovoRadioButton;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.sgproj.Config;
import com.lovo.sgproj.bean.ClassBean;
import com.lovo.sgproj.bean.RoomBean;
import com.lovo.sgproj.bean.StudentBean;
import com.lovo.sgproj.frame.MainFrame;
import com.lovo.sgproj.service.ClassService;
import com.lovo.sgproj.service.RoomService;
import com.lovo.sgproj.service.StudentService;
import com.lovo.sgproj.service.imp.ClassServiceImp;
import com.lovo.sgproj.service.imp.RoomServiceImp;
import com.lovo.sgproj.service.imp.StudentServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class StudentInDialog extends JDialog {

    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    private LovoTxt nameTxt;

    private LovoRadioButton genderBtn;

    private LovoComboBox<ClassBean> classCmb;// 后期需要把String改为班级Bean

    private LovoComboBox<RoomBean> roomCmb;// 后期需要把String改为房间Bean

    private LovoFileChooser headPic;

    private LovoTxt stuTelTxt;

    private LovoDate stuInTime;

    private LovoButton addBtn;

    private LovoButton cancelBtn;

    ArrayList<RoomBean> roomList = null;
    ArrayList<ClassBean> classList = null;

    private MainFrame frame;

    public StudentInDialog(MainFrame frame) {
        super(frame, "添加学生", true);
        this.frame = frame;
        this.setBounds(350, 250, 500, 400);
        this.setLayout(null);
        this.init();

        this.setVisible(true);
    }

    private void init() {
        // TODO Auto-generated method stub
        this.nameTxt = new LovoTxt("姓名", 20, 40, this);

        this.genderBtn = new LovoRadioButton("性别", new String[]{"男", "女"},
                20, 120, this) {
            @Override
            public void addListener(String itemName) {
                RoomService roomService = context.getBean(RoomServiceImp.class);
                if (itemName.equals("男")) {
                    roomList = roomService.showUserableRoom(true);
                    roomCmb.setList(roomList);
                } else {
                    roomList = roomService.showUserableRoom(false);
                    roomCmb.setList(roomList);
                }
            }
        };

        ClassService classService = context.getBean(ClassServiceImp.class);
        classList = classService.getAllClass();
        this.classCmb = new LovoComboBox<ClassBean>("班级", classList, 20, 200, this);
        classCmb.setList(classList);

        RoomService roomService = context.getBean(RoomServiceImp.class);
        roomList = roomService.showUserableRoom(true);
        this.roomCmb = new LovoComboBox<RoomBean>("房间", roomList, 20, 260, this);
        roomCmb.setList(roomList);

        this.headPic = new LovoFileChooser(this);
        this.headPic.setBounds(320, 20, 100, 150);

        this.stuTelTxt = new LovoTxt("联系电话", 250, 200, this);
        this.stuInTime = new LovoDate("入住时间", 250, 260, this);

        this.addBtn = new LovoButton("添加", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        this.addBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                StudentBean stu = new StudentBean();
                if (genderBtn.getItem().equals("男")) {
                    stu.setGender(true);
                } else {
                    stu.setGender(false);
                }
                stu.setTheClass(classCmb.getItem());
                stu.setTheRoom(roomCmb.getItem());
                stu.setStudentname(nameTxt.getText());
                //数据校验，即有效性数据
                //11位有效电话数字
                String a2 = "0?(13|14|15|17|18|19)[0-9]{9}";
                if (stuTelTxt.getText().matches(a2)) {
                    stu.setStudentphone(stuTelTxt.getText());
                } else {
                    JOptionPane.showMessageDialog(null, "请输入11位电话号码");
                }

                stu.setStudentdate(stuInTime.getDate());

                stu.setStudentimg(headPic.getFilePath());

                StudentService studentService = context.getBean(StudentServiceImp.class);
                studentService.studentInRoom(stu);

                frame.getStudentNode().addListener();
                StudentInDialog.this.dispose();
            }
        });

        this.cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                StudentInDialog.this.dispose();
            }
        });
    }

}
